package org.jaaksi.demo.touche;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View btn = findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // ！！！ setOnClickListener的onClick在
                // up事件触发后才会触发，如果View的不能触发up事件（down事件未消费），则也不会触发该回调
                Log.i("fcy", "Button.setOnClickListener ");
            }
        });

//        // !!!View的 OnTouchListener优先于onTouchEvent()对事件进行执行，默认返回false
//        // 如果返回true，则onToucheEvent不再触发，down事件会往上层传递
//        btn.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                boolean ret = false;
//                Log.i("fcy", "Button.setOnTouchListener onTouch "
//                    + ActionUtils.getAction(event) + " return " + ret);
//                return ret;
//            }
//        });
    }

    /**
     * Activity 中的dispatchTouchEvent调用了super,
     * 执行了getWindow().superDispatchTouchEvent(event),将事件传递给DecorView
     *
     * @param event
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
//        boolean ret = super.dispatchTouchEvent(event);
//        return false;
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            onUserInteraction();
        }
        if (getWindow().superDispatchTouchEvent(event)) {
            return true;
        }
        return onTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Return true if you have consumed the event, false if you haven't.
        // The default implementation always returns false.

        boolean ret = super.onTouchEvent(event);
        return ret;
    }
}
